﻿using JsonDemo.Core.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JsonDemo.Application.Services;


public class DemoService:IDemoServcie, ITransient
{
    private readonly IRepository<Demo> _repository;

    public DemoService(IRepository<Demo> repository)
    {
        _repository = repository;
    }


    public async Task<Demo> GetById(string Id)
    {
       var entity = await _repository.FirstOrDefaultAsync(x=>x.Id ==Id);

        return entity;
    }


    public async Task<int> Add(Demo demo)
    {
       await _repository.InsertAsync(demo);

        return  await _repository.SaveNowAsync();
    }

    public async Task<List<Demo>> list()
    {
      var lst =   await _repository.AsQueryable().ToListAsync();

        return lst;
    }

    public async Task<Demo> Update(Demo demo)
    {
       await _repository.UpdateAsync(demo);

        var effects = await _repository.SaveNowAsync();

        return await GetById(demo.Id);
    }
}
